<?php
// PdfRechnung geschrieben von Leonid Lezner. leolezner@yahoo.de

/* -------------------------------------------------------------------------------------
* 	ID:						$Id: pdf_bill.php 225 2013-09-22 10:21:51Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 225 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 10:21:51 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

define('PDF_MASTER_PASS', 'passworthiereingeben');

require(DIR_FS_DOCUMENT_ROOT.DIR_WS_CLASSES.'fpdf/fpdf_protection.php');
require_once(DIR_FS_INC.'inc.build_order_id.php');

$pdf = $db->db_query("	SELECT
							pdf_key,
							pdf_value
						FROM
							".TABLE_ORDERS_PDF_PROFILE."
						WHERE
							languages_id = '".$sprach_id."' ");

while (!$pdf->EOF){
	define($pdf->fields['pdf_key'], utf8_decode($pdf->fields['pdf_value']));
	$pdf->MoveNext();
}

class PDF_Bill extends FPDF_Protection {
	var $left_margin = 15;
	var $left_textoffset = 15;
	var $top_margin = 10;
	var $footer_y = -30;
	var $addresswindowmaxlen = 80;
	var $addresswindowtop = 50;
	var $fontfamily = 'Arial';
	var $logo_x = 135;
	var $logo_y = 10;
	var $rechnungsdaten_x = 135;
	var $rechnungsdaten_y = 67;
	var $rechnung_start = 100;
	var $pos_len = 10;
	var $menge_len = 15;
	var $artikel_len = 80;
	var $artikelnr_len = 20;
	var $einzelpreis_len = 20;
	var $preis_len = 23;
	var $pos_nr = 0;

	function Rechnungsdaten($kundennummer, $best_nr, $rech_nr, $datum, $zahlungsweise, $lieferschein) {
		$this->SetX($this->rechnungsdaten_x);
		$this->SetLeftMargin($this->rechnungsdaten_x);

		$this->SetY($this->rechnungsdaten_y);

		$this->SetFont($this->fontfamily,'', 10);

		$this->Cell(0, 6, TEXT_PDF_KUNDENNUMMER.': '.$kundennummer, 0, 1, '', 1);
		$this->Cell(0, 6, TEXT_PDF_BESTELLNUMMER.': '.build_order_id($datum, $best_nr, 'confirm'), 0, 1);
		if(!$lieferschein)
			$this->Cell(0, 6, TEXT_PDF_RECHNUNGSNUMMER.': '.$rech_nr, 0, 1, '', 1);
		else {
			$this->Cell(0, 6, TEXT_PDF_LIEFERSCHEIN.': '.build_order_id($datum, $best_nr, 'shipping'), 0, 1, '', 1);
		}

		$this->Cell(0, 6, TEXT_PDF_DATUM.': '.$datum, 0, 1);
		if($zahlungsweise != '')
			$this->Cell(0, 6, TEXT_PDF_ZAHLUNGSWEISE.': '.$zahlungsweise, 0, 1, '', 1);
	}

	function RechnungStart($rech_nr, $kundenname, $geschlecht, $lieferschein) {
		$this->SetX($this->left_textoffset);
		$this->SetLeftMargin($this->left_textoffset);
		$this->SetY($this->rechnung_start);

		$this->SetFont($this->fontfamily, 'B', 16);
		$this->Cell(0, 6, !$lieferschein ? TEXT_PDF_RECHNUNG.' '.$rech_nr : TEXT_PDF_LIEFERSCHEIN, 0, 1);
		$this->Ln();

		$this->SetFont($this->fontfamily, '', 10);

		switch($geschlecht) {
			case 'm':
				$message = TEXT_PDF_DANKE_MANN;
				break;
			case 'f':
				$message = TEXT_PDF_DANKE_FRAU;
				break;
			case 'u':
			default:
				$message = TEXT_PDF_DANKE_UNISEX;
		}

		$this->MultiCell(0, 6, sprintf($message, $kundenname), 0);
	}

	function ListeKopf($lieferschein) {
		$this->SetFont($this->fontfamily, 'B', 10);
		$this->Ln();
		if(!$lieferschein) {
			$this->Cell($this->pos_len, 6, TEXT_PDF_POSTEN.' ', 'B', 0, 'C');
			$this->Cell($this->menge_len, 6, TEXT_PDF_MENGE.' ', 'B', 0, 'C');
			$this->Cell($this->artikel_len, 6, TEXT_PDF_ARTIKEL, 'B', 0);
			$this->Cell($this->artikelnr_len, 6, TEXT_PDF_ARTIKELNR, 'B', 0);
			$this->Cell($this->einzelpreis_len, 6, TEXT_PDF_EINZELPREIS, 'B', 0, 'R');
			$this->Cell($this->preis_len, 6, TEXT_PDF_PREIS, 'B', 0, 'R');
		} else {
			$this->Cell($this->pos_len, 6, TEXT_PDF_POSTEN.' ', 'B', 0, 'C');
			$this->Cell($this->menge_len, 6, TEXT_PDF_MENGE.' ', 'B', 0, 'R');
			$this->Cell(125, 6, TEXT_PDF_ARTIKEL, 'B', 0);
			$this->Cell(30, 6, TEXT_PDF_ARTIKELNR, 'B', 0);
		}
		$this->Ln(8);
	}

	function ListeProduktHinzu($menge, $artikel, $zusinfos, $artnr, $zusinfoartnr, $einzelpreis, $preis, $lieferschein) {
		$this->SetFont($this->fontfamily,'', 10);
    $this->pos_nr++;

		$parts = preg_split("/[\s]+/", $artikel, -1, PREG_SPLIT_DELIM_CAPTURE); // Artikelbeschreibung splitten
		$line = 0;
		foreach($parts as $part) {
      $pos++;
			if($this->GetStringWidth($newtext.$part) < $this->artikel_len) {
				$newtext .= $lastpart.$part.' ';
				$lastpart = "";
			} else {
				if(!$lieferschein) {
					if($line == 0) {
						$this->Cell($this->pos_len, 6, sprintf('%02d', $this->pos_nr), 0, 0, 'C');
						$this->Cell($this->menge_len, 6, $menge.'x ', 0, 0, 'C');
					}
					else
						$this->Cell($this->pos_len+$this->menge_len, 6, '', 0);

					$this->Cell($this->artikel_len, 6, $newtext, 0);

					if($line == 0) {
						$this->SetFont($this->fontfamily,'', 9);
						$this->Cell($this->artikelnr_len, 6, $artnr, 0, 0, '');
						$this->SetFont($this->fontfamily,'', 10);
						$this->Cell($this->einzelpreis_len, 6, $einzelpreis, 0, 0, 'R');
						$this->Cell($this->preis_len, 6, $preis, 0, 0, 'R');
					} else {
						$this->SetFont($this->fontfamily,'', 9);
						$this->Cell($this->artikelnr_len, 6, '', 0);
						$this->SetFont($this->fontfamily,'', 10);
						$this->Cell($this->einzelpreis_len, 6, '', 0);
						$this->Cell($this->preis_len, 6, '', 0);
					}
				} else {
					if($line == 0) {
						$this->Cell($this->pos_len, 6, sprintf('%02d', $this->pos_nr), 0, 0, 'C');
						$this->Cell($this->menge_len, 6, $menge.'x ', 0, 0, 'C');
					}
					else
						$this->Cell($this->pos_len+$this->menge_len, 6, '', 0);

					$this->Cell(120, 6, $newtext, 0);

					if($line == 0) {
						$this->Cell(130, 6, $artnr, 0, 0, '');
					} else {
						$this->Cell(130, 6, '', 0);
					}
				}
				$this->Ln();
				$newtext = "";
				$lastpart = $part.' ';
				$line++;
			}
		}

		if($newtext) {
			if(!$lieferschein) {
				if($line == 0) {
					$this->Cell($this->pos_len, 6, sprintf('%02d', $this->pos_nr), 0, 0, 'C');
					$this->Cell($this->menge_len, 6, $menge.'x ', 0, 0, 'C');
				}
				else
					$this->Cell($this->pos_len+$this->menge_len, 6, '', 0);

				$this->Cell($this->artikel_len, 6, $newtext, 0);

				if($line == 0) {
					$this->SetFont($this->fontfamily,'', 9);
					$this->Cell($this->artikelnr_len, 6, $artnr, 0, 0, '');
					$this->SetFont($this->fontfamily,'', 10);
					$this->Cell($this->einzelpreis_len, 6, $einzelpreis, 0, 0, 'R');
					$this->Cell($this->preis_len, 6, $preis, 0, 0, 'R');
				} else {
					$this->Cell($this->artikelnr_len, 6, '', 0);
					$this->Cell($this->einzelpreis_len, 6, '', 0);
					$this->Cell($this->preis_len, 6, '', 0);
				}
			} else {
				if($line == 0) {
					$this->Cell($this->pos_len, 6, sprintf('%02d', $this->pos_nr), 0, 0, 'C');
					$this->Cell($this->menge_len, 6, $menge.'x ', 0, 0, 'C');
				}
        else
					$this->Cell($this->pos_len+$this->menge_len, 6, '', 1);

				$this->Cell(125, 6, $newtext, 0);

				if($line == 0)
					$this->Cell(30, 6, '', 0, 0, 'C');
				else
					$this->Cell(30, 6, '', 0);
			}
			$this->Ln();
		}

		if($zusinfos) {
			$this->SetFont($this->fontfamily, 'I', 9);

			$zusinfos_arr = explode("\n", $zusinfos);
			$zusinfoartnr_arr = explode("\n", $zusinfoartnr);
			
			if(!$lieferschein) {
				for($i = 0; $i < count($zusinfos_arr); $i++) {
					$this->Cell($this->pos_len+$this->menge_len, 5, '', 0, 0, '');
					$this->Cell($this->artikel_len, 5, $zusinfos_arr[$i], 0, 0, '');
	
					if($i < count($zusinfoartnr_arr))
						$this->Cell($this->artikelnr_len, 5, $zusinfoartnr_arr[$i], 0, 0, '');
	
					$this->Ln();
				}
				
			} else {
				for($i = 0; $i < count($zusinfos_arr); $i++) {
					$this->Cell($this->pos_len + $this->menge_len, 5, '', 0, 0, '');
					$this->Cell(130, 5, $zusinfos_arr[$i], 0, 0, '');
	
					if($i < count($zusinfoartnr_arr))
						$this->Cell($this->artikelnr_len, 5, $zusinfoartnr_arr[$i], 0, 0, '');
	
					$this->Ln();
				}
			}
		}
		$this->Ln(2);
	}

	function Betrag($orderdata, $lieferschein) {
		$this->SetFont($this->fontfamily, '', 10);

		$this->Cell($this->pos_len + $this->menge_len + $this->artikel_len + $this->einzelpreis_len + $this->artikelnr_len + $this->preis_len, 6, '', 'T');
		$this->Ln(2);

		foreach($orderdata as $info) {
			$text = $info['text'];
			$info['title'] = str_replace("::", ":", $info['title']);
			if ($info['class'] == 'ot_shipping') {
  			$info['title'] = utf8_encode(str_replace('€', 'Euro', utf8_decode($info['title'])));
      }

			if(strpos($text, "<b>") !== FALSE) {
				$this->SetFont($this->fontfamily, 'B', 10);

				$text = strip_tags($text);
				$info['title'] = strip_tags($info['title']);

				if($info['class'] == 'ot_total') {
					$this->Ln(2);

					$sum_len = 25;

					$this->Cell($this->pos_len + $this->menge_len + $this->artikel_len + $this->einzelpreis_len + $this->artikelnr_len - $sum_len, 1, "", '', 0);
					$this->Cell($this->preis_len + $sum_len - 1, 1, "", 'T', 1);
					$this->Cell($this->pos_len + $this->menge_len + $this->artikel_len + $this->einzelpreis_len + $this->artikelnr_len - $sum_len, 1, "", '', 0);
					$this->Cell($this->preis_len + $sum_len - 1, 1, "", 'T', 1);
				}
			} else if(strpos($text, "<font") !== FALSE) {
				$text = strip_tags($text);
				$this->SetTextColor(205,0,0);
			} else {
				$info['title'] = strip_tags($info['title']);
				$text = strip_tags($text);
				$this->SetFont($this->fontfamily, '', 10);
				$this->SetTextColor(0,0,0);
			}
			if($info['class'] == 'ot_total')
				$this->SetFont($this->fontfamily, 'B', 11);
			else
				$this->SetFont($this->fontfamily, '', 10);

			$this->Cell($this->pos_len + $this->menge_len + $this->artikel_len + $this->einzelpreis_len + $this->artikelnr_len, 6, $info['title'], 0, 0, 'R');
			$this->Cell($this->preis_len, 6, $text, 0, 1, 'R');
		}
	}

	function Kommentar($comment, $method, $lieferschein) {
		if(!$lieferschein) {
			if(!$comment)
				return;

			$this->Ln(10);
			$text = TEXT_PDF_KOMMENTAR;
		} else {
			$this->Ln(10);
			$text = str_replace('#vm#', $method, TEXT_PDF_DELIVERY_SCHLUSSTEXT);
		}
		$this->SetFont($this->fontfamily, 'B', 10);
		$this->Cell($this->preis_len, 10, $text, 0, 1);

		if($lieferschein)
			return;

		$this->SetFont($this->fontfamily, '', 10);
		$this->MultiCell(0, 4, utf8_decode($comment));
	}

	function RechnungEnde($type = '') {
		global $order;
		if($type == 'lieferschein')
			return;
		$this->Ln(10);
		$this->SetFont($this->fontfamily, '', 10);
		if(!empty($order->customer['vat_id']))
			$this->MultiCell(0, 6, 'Ihre USt-ID: '.$order->customer['vat_id']);
		$this->MultiCell(0, 6, TEXT_PDF_SCHLUSSTEXT);
	}

	function Footer() {
		$this->SetFont($this->fontfamily,'',8);
		$this->SetTextColor(128);
		$left = $this->PutInfoBlock(0, TEXT_PDF_ANSCHRIFT, $this->footer_y);
		$left = $this->PutInfoBlock($left, TEXT_PDF_KONTAKT, $this->footer_y);
		$left = $this->PutInfoBlock($left, TEXT_PDF_BANK, $this->footer_y);
		$left = $this->PutInfoBlock($left, TEXT_PDF_GESCHAEFT, $this->footer_y);

		$this->Line(5, 297.0/2.0, 8, 297.0/2.0);
	}

	function Init($title) {
		$this->SetAutoPageBreak(true, abs($this->footer_y) + 1);
		$this->SetCreator(STORE_NAME . ' &copy; ' . date("Y"));
		$this->AliasNbPages();
		$this->AddPage();
		$this->SetFillColor(230,230,230);
		$this->SetDisplayMode('fullwidth');
		$this->SetTitle($title);
		// $this->SetProtection(array('print'),'', PDF_MASTER_PASS); // nur Drucken erlaubt, kein User-Passwort, jedoch ein Master-Passwort
	}

	function Header() {
		$this->SetFont($this->fontfamily,'' ,9);

		$this->SetX($this->left_margin);
		$this->SetLeftMargin($this->left_margin);

		// Ausgabe des Headertextes
		$this->Cell(0, 4, TEXT_PDF_HEADER, 0, 1);
		// Schiebe die Zeile auf die Anfangsposition
		$this->SetY($this->top_margin);
		// Ausgabe der Seitenzahl in grau, kursiv und kleiner
		$this->SetTextColor(128);
		$this->SetFont('Arial','I',8);
		$this->Cell(0, 4, TEXT_PDF_SEITE.' '.$this->PageNo().' '.TEXT_PDF_SEITE_VON.' {nb}', 0, 0, 'R');

		// Für die nachfolgenden Seiten der Abstand vom Header
		$this->Ln();
		$this->Ln();
	}

	function maxlen($strings) {
		$max = 0;
		for ($i = 0; $i < count($strings); $i++) {
			if($this->GetStringWidth($strings[$i]) > $max)
				$max = $this->GetStringWidth($strings[$i]);
		}
		return $max + 6;
	}

	function PutInfoBlock($left, $body, $y) {
		$this->SetFont($this->fontfamily,'',6);

		$this->SetY($y);

		$body_arr = explode("\n", $body);
		$maxlen = $this->maxlen($body_arr);

		$this->SetLeftMargin($left + $this->left_margin);
		$this->SetX($left + $this->left_margin);

		$this->MultiCell($maxlen, 4, $body, 'T');

		return $left + $maxlen;
	}

	function Adresse($kundenadresse, $geschaeftsadresse) {
		// Kundenadresse ausgeben
		$this->SetX($this->left_textoffset + 5);
		$this->SetLeftMargin($this->left_textoffset + 5);

		$this->SetY($this->addresswindowtop + 7);

		$this->SetFont($this->fontfamily,'',11); // normal
		$this->MultiCell($this->addresswindowmaxlen - 5, 4, $kundenadresse);

		// Shopadresse ausgeben
		$this->SetX($this->left_textoffset + 5);
		$this->SetLeftMargin($this->left_textoffset + 5);

		$this->SetY($this->addresswindowtop);

		$this->SetFont($this->fontfamily,'',5); // klein
		$this->MultiCell($this->addresswindowmaxlen, 2.5, $geschaeftsadresse);

	}

	function Logo($pfad) {
		$size = getimagesize($pfad);
		$this->Image($pfad, $this->logo_x, $this->logo_y, $size[0] / 80 * 25.4);
	}
}
?>